package io.dbw.common.utils;

import com.alibaba.fastjson.JSON;
import io.dbw.modules.business.dto.BooksDTO;
import org.apache.poi.ss.formula.functions.T;

import java.lang.reflect.Field;
import java.util.Map;

public class MapToDTOUtil {
    public static void main(String[] args) {
        BooksDTO booksDTO = new BooksDTO();
        Map<String, Object> stringObjectMap = dtoTOMap(booksDTO);
        stringObjectMap.put("d","d");
        System.out.println(stringObjectMap);
    }

    public static Map<String,Object> dtoTOMap(Object t){
        Map<String,Object> map = JSON.parseObject(JSON.toJSONString(t), Map.class);
        return map;
    }


    public static <T> T mapToEntity(Map<String, Object> map, Class<T> clazz) {
        try {
            T entity = clazz.newInstance(); // 创建实体类对象
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                field.set(entity, map.get(field.getName())); // 设置实体类属性值
            }
            return entity;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
}
